function QuestionRule() {
    var _this = this;
    var _packages;
    _this.businessIds = [];

    _this.ajax = function(url, param, type) {
        var result;
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : param,
            success : function(data) {
                result = data;
            }
        });
        return result;
    };


    // 初始化事件
    _this.initEvent = function () {
        $('#select-input').click(function () {
            $("#search_tree").show();
            $("#search_tree").fadeIn();
        })
        $('#search_tree').mouseleave(function(){
            $("#search_tree").hide();
        })
    }

    _this.init_default= function(){
        var url = appPath + "/super/project/coursePackage/selectQuestionRules";
        var param = {"projectId" : $("#projectId").val()};
        var data = _this.ajax(url, param);
        var code = data['code'];
        if(code == '10000') {
            _packages = data['result'];
            var question_rule_list = _this.init_table(_packages);
            $("#question_rule_list").html(question_rule_list);

            _this.loadEditable();
            _this.ruleListener();
            _this.initEvent();
        }
    }


    _this.init_table= function(packages){
        var question_rule_list = "";
        for(var i=0;i<packages.length;i++){
            var package = packages[i];
            var singleCount = package.singleDifficult1Count + package.singleDifficult2Count + package.singleDifficult3Count
                                + package.singleDifficult4Count + package.singleDifficult5Count;
            var singleAllCount = package.singleDifficult1AllCount + package.singleDifficult2AllCount + package.singleDifficult3AllCount
                                + package.singleDifficult4AllCount + package.singleDifficult5AllCount;
            var manyCount = package.manyDifficult1Count + package.manyDifficult2Count + package.manyDifficult3Count
                                + package.manyDifficult4Count + package.manyDifficult5Count;
            var manyAllCount = package.manyDifficult1AllCount + package.manyDifficult2AllCount + package.manyDifficult3AllCount
                                + package.manyDifficult4AllCount + package.manyDifficult5AllCount;
            var judgeCount = package.judgeDifficult1Count + package.judgeDifficult2Count + package.judgeDifficult3Count
                                + package.judgeDifficult4Count + package.judgeDifficult5Count;
            var judgeAllCount = package.judgeDifficult1AllCount + package.judgeDifficult2AllCount + package.judgeDifficult3AllCount
                                + package.judgeDifficult4AllCount + package.judgeDifficult5AllCount;
            var count = singleCount + manyCount + judgeCount;
            var allCount = singleAllCount + manyAllCount + judgeAllCount;

            question_rule_list += '<tr>';
            question_rule_list += '<td rowspan="15">'+(i+1)+'</td>';
            question_rule_list += '<td rowspan="15">'+package.packageName;
            question_rule_list += '<input type="text" data-max="'+allCount+'" name="count" id="count_'+i+'" data-value="'+count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+allCount+'】）</span>';

            question_rule_list += '</td>';
            question_rule_list += '<td rowspan="5">单选题';
            question_rule_list += '<input type="text" data-max="'+singleAllCount+'" name="singleCount" id="singleCount_'+i+'" data-value="'+singleCount+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+singleAllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '<td>易';
            question_rule_list += '<input type="text" data-max="'+package.singleDifficult1AllCount+'" name="singleDifficult1Count" id="singleDifficult1Count_'+i+'" data-value="'+package.singleDifficult1Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.singleDifficult1AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较易';
            question_rule_list += '<input type="text" data-max="'+package.singleDifficult2AllCount+'" name="singleDifficult2Count" id="singleDifficult2Count_'+i+'" data-value="'+package.singleDifficult2Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.singleDifficult2AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>中等';
            question_rule_list += '<input type="text" data-max="'+package.singleDifficult3AllCount+'" name="singleDifficult3Count" id="singleDifficult3Count_'+i+'" data-value="'+package.singleDifficult3Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.singleDifficult3AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较难';
            question_rule_list += '<input type="text" data-max="'+package.singleDifficult4AllCount+'" name="singleDifficult4Count" id="singleDifficult4Count_'+i+'" data-value="'+package.singleDifficult4Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.singleDifficult4AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>难';
            question_rule_list += '<input type="text" data-max="'+package.singleDifficult5AllCount+'" name="singleDifficult5Count" id="singleDifficult5Count_'+i+'" data-value="'+package.singleDifficult5Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.singleDifficult5AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td rowspan="5">多选题';
            question_rule_list += '<input type="text" data-max="'+manyAllCount+'" name="manyCount" id="manyCount_'+i+'" data-value="'+manyCount+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+manyAllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '<td>易';
            question_rule_list += '<input type="text" data-max="'+package.manyDifficult1AllCount+'" name="manyDifficult1Count" id="manyDifficult1Count_'+i+'" data-value="'+package.manyDifficult1Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.manyDifficult1AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较易';
            question_rule_list += '<input type="text" data-max="'+package.manyDifficult2AllCount+'" name="manyDifficult2Count" id="manyDifficult2Count_'+i+'" data-value="'+package.manyDifficult2Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.manyDifficult2AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>中等';
            question_rule_list += '<input type="text" data-max="'+package.manyDifficult3AllCount+'" name="manyDifficult3Count" id="manyDifficult3Count_'+i+'" data-value="'+package.manyDifficult3Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.manyDifficult3AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较难';
            question_rule_list += '<input type="text" data-max="'+package.manyDifficult4AllCount+'" name="manyDifficult4Count" id="manyDifficult4Count_'+i+'" data-value="'+package.manyDifficult4Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.manyDifficult4AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>难';
            question_rule_list += '<input type="text" data-max="'+package.manyDifficult5AllCount+'" name="manyDifficult5Count" id="manyDifficult5Count_'+i+'" data-value="'+package.manyDifficult5Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.manyDifficult5AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td rowspan="5">判断题';
            question_rule_list += '<input type="text" data-max="'+judgeAllCount+'" name="judgeCount" id="judgeCount_'+i+'" data-value="'+judgeCount+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+judgeAllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '<td>易';
            question_rule_list += '<input type="text" data-max="'+package.judgeDifficult1AllCount+'" name="judgeDifficult1Count" id="judgeDifficult1Count_'+i+'" data-value="'+package.judgeDifficult1Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.judgeDifficult1AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较易';
            question_rule_list += '<input type="text" data-max="'+package.judgeDifficult2AllCount+'" name="judgeDifficult2Count" id="judgeDifficult2Count_'+i+'" data-value="'+package.judgeDifficult2Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.judgeDifficult2AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>中等';
            question_rule_list += '<input type="text" data-max="'+package.judgeDifficult3AllCount+'" name="judgeDifficult3Count" id="judgeDifficult3Count_'+i+'" data-value="'+package.judgeDifficult3Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.judgeDifficult3AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>较难';
            question_rule_list += '<input type="text" data-max="'+package.judgeDifficult4AllCount+'" name="judgeDifficult4Count" id="judgeDifficult4Count_'+i+'" data-value="'+package.judgeDifficult4Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.judgeDifficult4AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';

            question_rule_list += '<tr>';
            question_rule_list += '<td>难';
            question_rule_list += '<input type="text" data-max="'+package.judgeDifficult5AllCount+'" name="judgeDifficult5Count" id="judgeDifficult5Count_'+i+'" data-value="'+package.judgeDifficult5Count+'" style="width: 35px;" readonly>';
            question_rule_list += '<br/><span>（总题量=【'+package.judgeDifficult5AllCount+'】）</span>';
            question_rule_list += '</td>';
            question_rule_list += '</tr>';
        }
        return question_rule_list;
    }

    _this.save = function(){
        var flag = _this.validateCount();
        if(flag=="true"){
            var packageList = new Array();
            for(var i=0;i<_packages.length;i++){
                var package = _packages[i];
                var entity = {
                    "id" : package.id,
                    "singleDifficult1Count" : $("#singleDifficult1Count_"+i).val(),
                    "singleDifficult2Count" : $("#singleDifficult2Count_"+i).val(),
                    "singleDifficult3Count" : $("#singleDifficult3Count_"+i).val(),
                    "singleDifficult4Count" : $("#singleDifficult4Count_"+i).val(),
                    "singleDifficult5Count" : $("#singleDifficult5Count_"+i).val(),
                    "manyDifficult1Count" : $("#manyDifficult1Count_"+i).val(),
                    "manyDifficult2Count" : $("#manyDifficult2Count_"+i).val(),
                    "manyDifficult3Count" : $("#manyDifficult3Count_"+i).val(),
                    "manyDifficult4Count" : $("#manyDifficult4Count_"+i).val(),
                    "manyDifficult5Count" : $("#manyDifficult5Count_"+i).val(),
                    "judgeDifficult1Count" : $("#judgeDifficult1Count_"+i).val(),
                    "judgeDifficult2Count" : $("#judgeDifficult2Count_"+i).val(),
                    "judgeDifficult3Count" : $("#judgeDifficult3Count_"+i).val(),
                    "judgeDifficult4Count" : $("#judgeDifficult4Count_"+i).val(),
                    "judgeDifficult5Count" : $("#judgeDifficult5Count_"+i).val()
                };
                packageList.push(entity);
            }

            var url = appPath + "/super/project/coursePackage/updateBatch";
            var param = {
                "packageList" : JSON.stringify(packageList),
                "mustQuestionRule" : JSON.stringify({
                    "mustQuestionCount": $("#mustQuestionCount").val(),
                    "coefficient": $("#coefficient").val(),
                    "projectId": $("#projectId").val(),
                    "businessId": $("#businessId").val(),
                    "businessName": $("#select-input").val()
                })
            };
            var data = _this.ajax(url, param);
            var code = data['code'];
            if(code == '10000') {
                layer.msg('操作成功', {icon: 1});
                return true;
            }
        } else {
            layer.alert(flag, {icon: 2,  skin: 'layer-ext-moon'});
            return false;
        }
    }

    _this.nextStep = function(){
        if(_this.save()){
            var url = appPath + "/super/project/coursePackage/createQuestions";
            var param = {"projectId" : $("#projectId").val()};
            var data = _this.ajax(url, param);
            var code = data['code'];
            if(code == '10000') {
                layer.alert('操作成功', {icon: 1,  skin: 'layer-ext-moon', closeBtn: 0}, function(index){
                    layer.close(index);
                    parent.self_select_question.initTable();
                    parent.layer.close(parent.layer.getFrameIndex(window.name));

                });
            }
        }
    }

    //初始化插件
    _this.loadEditable = function () {
        $("form[id^=question_rule_form]").find("input:not([type=hidden])").each(function () {
            $(this).editable({
                validate:function (value) {
                    if(isNaN(value)){
                        return  '输入不合法';
                    }
                    if(value == ""){
                        value = 0 ;
                    }

                    if(Number(value) < 0){
                        return  '不能输入负数';
                    }

                    if(Number(value) != parseInt(value)){
                        return  '不能输入小数';
                    }

                    if(Number(value)%($("#coefficient").val())!=0){
                        return  '试题分值必须是'+$("#coefficient").val()+'的整数倍';
                    }

                    var max = $(this).attr("data-max");
                    var min = $(this).attr("data-min");
                    if(Number(value) > Number(max)){
                        return  '最大值为' + max;
                    }
                    if(Number(value) < Number(min)){
                        return  '最小值为' + min;
                    }
                },
                display:function (value) {
                    if(value == ""){
                        $(this).val(0);
                    }else{
                        $(this).val(value);
                    }
                    $(this).trigger("keyup");
                }
            })
        });

        $("form[id^=question_must_form]").find("input:not([type=hidden])").each(function () {
            $(this).editable({
                validate:function (value) {
                    if(isNaN(value)){
                        return  '输入不合法';
                    }
                    if(value == ""){
                        value = 0 ;
                    }

                    if(Number(value) < 0){
                        return  '不能输入负数';
                    }

                    if(Number(value) != parseInt(value)){
                        return  '不能输入小数';
                    }
                },
                display:function (value) {
                    if(value == ""){
                        $(this).val(0);
                    }else{
                        $(this).val(value);
                    }
                    $(this).trigger("keyup");
                }
            })
        });
    };


    //增加监听
    _this.ruleListener = function(){
        $("form[id^=question_rule_form]").on("click", "input", function(){
            if(Number($("#mustQuestionCount").val())==0){
                layer.alert("必考题题量不能为0", {icon: 2,  skin: 'layer-ext-moon'});
                return;
            }
            if(Number($("#coefficient").val())==0){
                layer.alert("系数不能为0", {icon: 2,  skin: 'layer-ext-moon'});
                return;
            }
        });

        $("form[id^=question_must_form]").on("keyup", "input", function(){
            $("#mustQuestionAllCount").html(Number($("#mustQuestionCount").val())*Number($("#coefficient").val()));
        });
    };

    _this.validateCount = function(){

        if(Number($("#businessId").val())==''){
            return "行业不能为空";
        }
        if(Number($("#mustQuestionCount").val())==0){
            return "必考题题量不能为0";
        }
        if(Number($("#coefficient").val())==0){
            return "系数不能为0";
        }

        var count = 0;
        for(var i=0;i<_packages.length;i++){
            count += Number($("#count_"+i).val());

            var packagename = _packages[i].packageName;
            if(Number($("#count_"+i).val()) != Number($("#singleCount_"+i).val())+Number($("#manyCount_"+i).val())+Number($("#judgeCount_"+i).val())){
                return "课程包【"+packagename+"】总数量错误";
            }
            if(Number($("#singleCount_"+i).val()) != Number($("#singleDifficult1Count_"+i).val()) + Number($("#singleDifficult2Count_"+i).val()) + Number($("#singleDifficult3Count_"+i).val())
                + Number($("#singleDifficult4Count_"+i).val()) + Number($("#singleDifficult5Count_"+i).val())){
                return "课程包【"+packagename+"】单选题数量错误";
            }
            if(Number($("#manyCount_"+i).val()) != Number($("#manyDifficult1Count_"+i).val()) + Number($("#manyDifficult2Count_"+i).val()) + Number($("#manyDifficult3Count_"+i).val())
                + Number($("#manyDifficult4Count_"+i).val()) + Number($("#manyDifficult5Count_"+i).val())){
                return "课程包【"+packagename+"】多选题数量错误";
            }
            if(Number($("#judgeCount_"+i).val()) != Number($("#judgeDifficult1Count_"+i).val()) + Number($("#judgeDifficult2Count_"+i).val()) + Number($("#judgeDifficult3Count_"+i).val())
                + Number($("#judgeDifficult4Count_"+i).val()) + Number($("#judgeDifficult5Count_"+i).val())){
                return "课程包【"+packagename+"】判断题数量错误";
            }
        }
        if(count == $("#mustQuestionCount").val()*$("#coefficient").val()){
            return "true";
        } else {
            return "选题数量错误，自动选题数量=必考题题量*系数";
        }
    };
}

var questionRule = new QuestionRule();